/*
 * @Author: wz
 * @Date: 2021-01-29 15:08:31
 * @LastEditors: wz
 * @LastEditTime: 2021-01-29 15:08:46
 * @Description:
 */

import Vue from 'vue'

const mixins = Vue.mixin({
    methods: {
        // 通用表格增行
        minxin_addRow(data = [], field = {}, index, mergeTable) {
            if (!Array.isArray(data) || typeof field !== 'object') return
            if (index) {
                data.splice(index, 0, { ...field })
                return data
            }
            return data.push({ ...field })
        },

        // 通用表格减行
        minxin_delRow(data = [], index) {
            if (!Array.isArray(data)) return
            let flag = false
            this.$confirm('是否真的删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    flag = data.splice(index, 1)
                })
                .catch(() => {
                    flag = false
                })
            return flag
        },

        // 性能测试
        performance() {
            let { memory, timing } = window.performance
            const {
                domainLookupEnd,
                domainLookupStart,
                connectEnd,
                connectStart,
                responseEnd,
                responseStart,
                domComplete,
                domInteractive,
                navigationStart
            } = timing
            console.log('内存大小----', memory.jsHeapSizeLimit / 1024 / 1024 + 'MB')
            console.log('可使用内存----', memory.totalJSHeapSize / 1024 / 1024 + 'MB')
            console.log('占用内存----', memory.usedJSHeapSize / 1024 / 1024 + 'MB')
            console.log('DNS查询耗时----', (domainLookupEnd - domainLookupStart) / 1000 + 's')
            console.log('TCP链接耗时----', (connectEnd - connectStart) / 1000 + 's')
            console.log('request请求耗时----', (responseEnd - responseStart) / 1000 + 's')
            console.log('解析dom树耗时----', (domComplete - domInteractive) / 1000 + 's')
            console.log('白屏时间----', (responseStart - navigationStart) / 1000 + 's')
        }
    }
})
export default { mixins }
